<?php

namespace app\controllers\h5;

use app\controllers\h5\base\H5Controller;
use app\models\user\User;
use app\models\user\UserLevel;


class RankController extends H5Controller
{
    public function actionList()
    {
        // $page = intval($this->post('page', $this->page));
        // $pageSize = intval($this->post('pageSize', $this->pageSize));

        $query = User::find()
            ->where(['>', 'referrer_id', 0])
            ->andWhere(['>=', 'level', UserLevel::$keys['vip1']])
            ->select('referrer_id, count(referrer_id) as c')
            ->groupBy('referrer_id')
            ->orderBy(['c' => SORT_DESC])
            ->limit(10);

        // var_dump($query->createCommand()->getRawSql());exit;

        // $totalCount = $query->count();
        // $totalPage = ceil($totalCount / $pageSize);

        // $list = $query->offset(($page - 1) * $pageSize)
        //     ->limit($pageSize)
        //     ->asArray()
        //     ->all();

        $list = $query->asArray()->all();

        foreach ($list as &$item) {

            $item['user'] = User::findOne(['id' => $item['referrer_id']])->toArray();


            $item['user']['nickname'] = base64_decode($item['user']['nickname']);

            if ($item['user']['base64Image']) {
                $item['user']['avatar'] = $item['user']['base64Image'];
            }
        }

        return $this->successEncode([
            $this->list => $list,
            // $this->totalCount => (int)$totalCount,
            // $this->totalPage => $totalPage,
            // $this->currentPage => $page
        ]);
    }
}